package com.ruoyi.pcwl.controller;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.pcwl.domain.AppCarousel;
import com.ruoyi.pcwl.domain.MediaFiles;
import com.ruoyi.pcwl.domain.dto.AppCarouselDto;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.IAppCarouselService;
import com.ruoyi.pcwl.service.IMediaFilesService;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 轮播图信息Controller
 *
 * @author ruoyi
 * @date 2024-01-19
 */
//@Api("轮播图管理接口")
@RestController
@RequestMapping("/appCarousel")
public class AppCarouselController extends BaseController
{
    @Autowired
    private IAppCarouselService appCarouselService;

    @Autowired
    IMediaFilesService mediaFilesService;

    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     * 查询轮播图信息列表
     */
    @ApiOperation("分页获取轮播图列表")
//    @PreAuthorize("@ss.hasPermi('system:carousel:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppCarousel appCarousel)
    {

        Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum")==null?1:ServletUtils.getParameter("pageNum"));
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize")==null?10:ServletUtils.getParameter("pageSize"));
        Page<AppCarousel> appCarouselPage = new Page<>(pageNum, pageSize);

//        IPage<AppCarousel> appCarouselIPage = appCarouselService.getArticlePage(appCarouselPage,appCarousel);

        IPage<AppCarousel> articlePage = appCarouselService.getArticlePage(appCarouselPage, appCarousel);
        return getDataTable(articlePage);
    }

    /**
     * 导出轮播图信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:carousel:export')")
    @Log(title = "轮播图信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppCarousel appCarousel)
    {
        List<AppCarousel> list = appCarouselService.selectAppCarouselList(appCarousel);
        ExcelUtil<AppCarousel> util = new ExcelUtil<AppCarousel>(AppCarousel.class);
        util.exportExcel(response, list, "轮播图信息数据");
    }

    /**
     * 获取轮播图信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:carousel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appCarouselService.selectAppCarouselById(id));
    }


    /**
     * 新增轮播图信息
     */
//    @PreAuthorize("@ss.hasPermi('system:carousel:add')")
//    @Log(title = "轮播图信息", businessType = BusinessType.INSERT)
    @PostMapping("/addCarousel")
    public AjaxResult addCarousel(AppCarouselDto appCarouselDto) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException {

        //上传文件到minio
        Map<String, Object> map = MinioUtil.uploadFile(appCarouselDto.getFile(), MediaTypeEnum.CAROUSEL);

        //保存轮播图信息
        AppCarousel appCarousel = new AppCarousel();
        BeanUtils.copyProperties(appCarouselDto,appCarousel);
        appCarousel.setPic(map.get("path").toString());
        appCarousel.setMediaFileId(Long.parseLong(map.get("mediaFileId").toString()));
        appCarouselService.save(appCarousel);

        return AjaxResult.success();
    }


    /**
     * 修改轮播图信息
     */
//    @PreAuthorize("@ss.hasPermi('system:carousel:edit')")
    @Log(title = "轮播图信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppCarousel appCarousel)
    {
        return toAjax(appCarouselService.updateAppCarousel(appCarousel));
    }

    /**
     * 删除轮播图信息
     */
//    @PreAuthorize("@ss.hasPermi('system:carousel:remove')")
//    @Log(title = "轮播图信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appCarouselService.deleteAppCarouselByIds(ids));
    }


    @ApiOperation("发布轮播到小程序")
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @PutMapping("/publishArticle/{id}")
    public AjaxResult publishCarousel(@PathVariable("id") String id) {
        appCarouselService.publishCarousel(id);
        return success();
    }


    public TableDataInfo getDataTable(IPage page)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}
